package com.project.bookstore.user.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.project.bookstore.common.CommonResult;
import com.project.bookstore.user.pojo.*;
import com.project.bookstore.user.service.AddressService;
import com.project.bookstore.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Api(tags = "用户模块")
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private AddressService addAddress;


    @ApiOperation("个人中心Info页")
    @ApiOperationSupport(order = 110)
    @GetMapping("/mine/{id}")
    public CommonResult<MineInfoVO> mineInfo(@PathVariable Long id) {
        log.debug("mineInfo( {} )", id);
        return userService.mineInfo(id);
    }

    @ApiOperation("获取个人详细信息")
    @ApiOperationSupport(order = 111)
    @GetMapping("/personalData/{id}")
    public CommonResult<UserPersonalDataVO> getPersonalData(@PathVariable Long id) {
        return userService.getPersonalData(id);
    }

    @ApiOperation("用户名查重")
    @ApiOperationSupport(order = 112)
    @GetMapping("/username/{username}")
    public CommonResult getPersonalData(@PathVariable String username) {
        return userService.checkIsUserNameExist(username);
    }

    @ApiOperation("获取模拟手机验证码")
    @ApiOperationSupport(order = 113)
    @GetMapping("/getVerificationCode/{phone}")
    public CommonResult<String> getVerificationCode(@PathVariable String phone) {
        return userService.getVerificationCode(phone);
    }

    @ApiOperation("验证密码")
    @ApiOperationSupport(order = 114)
    @GetMapping("/password/{id}/{password}")
    public CommonResult<Void> checkPassword(
            @PathVariable("id") Long id,
            @PathVariable("password") String password) {
        return userService.checkPassword(id, password);
    }

    @ApiOperation("验证模拟手机验证码")
    @ApiOperationSupport(order = 115)
    @GetMapping("/checkVerificationCode/{phone}/{verificationCode}")
    public CommonResult<Void> checkPassword(
            @PathVariable("phone") String phone,
            @PathVariable("verificationCode") String verificationCode) {
        return userService.checkVerificationCode(phone, verificationCode);
    }

    @ApiOperation("获取手机号")
    @ApiOperationSupport(order = 116)
    @GetMapping("/phone/{id}")
    public CommonResult<String > getPhoneById(@PathVariable Long id){
        return userService.getPhoneById(id);
    }

    @ApiOperation("获取邮箱")
    @ApiOperationSupport(order = 117)
    @GetMapping("/email/{id}")
    public CommonResult<String > getEmailById(@PathVariable Long id){
        return userService.getEmailById(id);
    }

    @ApiOperation("登录状态请求")
    @ApiOperationSupport(order = 118)
    @GetMapping("/state/{id}")
    public CommonResult<Void> getState(@PathVariable Long id, HttpServletRequest request) {
        return userService.getState(id,request);
    }

    @ApiOperation("登录后菜单栏的头像、昵称信息")
    @ApiOperationSupport(order = 119)
    @GetMapping("/menuSource/{id}")
    public CommonResult<UserMenuPersonalSourceVO> getMenuPersonalSource(@PathVariable Long id) {
        return userService.getMenuPersonalSource(id);
    }

    @ApiOperation("登出")
    @ApiOperationSupport(order = 120)
    @GetMapping("/logout/{id}")
    public CommonResult<Void> logout(@PathVariable Long id) {
        return userService.logout(id);
    }

    @ApiOperation("获取地址")
    @ApiOperationSupport(order = 121)
    @GetMapping("/address/{userId}")
    public CommonResult<List<AddressForPayVO>> getAddressForPay(@PathVariable Long userId){
        return userService.getAddressForPay(userId);
    }



    @ApiOperation(value = "登录", notes = "测试可用用户名 123,密码 123 获得认证 token,添加至请求头 Authentication,方可进行测试")
    @ApiOperationSupport(order = 210)
    @ApiParam(name = "loginVO", value = "用户名和密码(测试可用123,123)", required = true)
    @PostMapping("/login")
    public CommonResult<UserLoginVO> login(@RequestBody LoginVO user,HttpServletRequest request) {
        log.debug("controller.login( {} ),request:【{}】", user,request);
        return userService.login(user,request);
    }

    @ApiOperation("上传头像")
    @ApiOperationSupport(order = 211)
    @PostMapping("/avatar")
    public String uploadImg(MultipartFile userImg) throws IOException {
        return userService.updateAvatar(userImg);
    }

    @ApiOperation("修改个人资料")
    @ApiOperationSupport(order = 212)
    @PostMapping("/personalData")
    public CommonResult<Void> updatePersonalData(@RequestBody UserPersonalDataVO user) {
        System.out.println(user);
        return userService.updatePersonalData(user);
    }


    @ApiOperation("修改密码")
    @ApiOperationSupport(order = 213)
    @PostMapping("/password")
    public CommonResult<Void> updatePassword(@RequestBody UserPasswordDTO userSe) {
        return userService.updatePassword(userSe.getId(),
                userSe.getPassword(), userSe.getOldPass());
    }

    @ApiOperation("注册")
    @ApiOperationSupport(order = 214)
    @PostMapping("/register")
    public CommonResult register(@RequestBody RegisterVO registerVO) {
        return userService.register(registerVO);
    }

    @ApiOperation("修改手机号")
    @ApiOperationSupport(order = 215)
    @PostMapping("/phone/{id}")
    public CommonResult updatePassword(
            @PathVariable Long id,
            @RequestBody String phone) {
        return userService.updatePhone(id, phone);
    }

    @ApiOperation("修改邮箱")
    @ApiOperationSupport(order = 216)
    @PostMapping("/email/{id}")
    public CommonResult updateEmail(
            @PathVariable Long id,
            @RequestBody String email) {
        System.out.println("id = " + id + ", email = " + email);
        return userService.updateEmail(id, email);
    }

    @ApiOperation("修改昵称")
    @ApiOperationSupport(order = 217)
    @PostMapping("/nick/{userId}")
    public CommonResult updateNick(@PathVariable Long userId,
            @RequestBody String nick){
        return userService.updateNick(userId,nick);
    }

    @ApiOperation("新增地址")
    @ApiOperationSupport(order = 218)
    @PostMapping("/address/{userId}")
    public CommonResult addAddress(@PathVariable Long userId,
                                   @RequestBody AddressCreateVO addressCreateVO){
        return addAddress.addAddress(userId,addressCreateVO);
    }

    @ApiOperation("修改地址")
    @ApiOperationSupport(order = 219)
    @PostMapping("/address/update/{addressId}")
    public CommonResult updateAddress(@PathVariable Long addressId,
                                   @RequestBody AddressCreateVO addressCreateVO){
        return addAddress.updateAdd(addressId,addressCreateVO);
    }
}
